home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / xview / genial / func / state_mach.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-14  |  8.2 KB  |  393 lines

  1.  
  2. #include <stdio.h>
  3. #include "display.h"
  4. #include "ui.h"
  5. #include "sm.h"
  6. #include "log.h"
  7. #include "reg.h"
  8.  
  9. static int state;
  10.  
  11. init_sm()
  12. {
  13.     int       main_dispatch();
  14.  
  15.     state = IMG_UNLOADED;
  16.     set_panel_state(state);
  17.     dp_push(main_dispatch);
  18. }
  19.  
  20. new_state(ns)
  21.     int       ns;
  22. {
  23.     state = ns;
  24.     set_panel_state(state);
  25. }
  26.  
  27. int
  28. getstate()
  29. {
  30.     return state;
  31. }
  32.  
  33. /* main_dispatch() is the main dispatch loop for GENIAL */
  34. int
  35. main_dispatch(token, arg)
  36.     int       token;        /* token as defined in sm.h */
  37.     caddr_t  *arg;        /* pointer to an optional argument */
  38. {
  39.     XPoint    pt;        /* x and y *IMAGE* coordinates of the event */
  40.  
  41. #ifdef DEBUG
  42.     printf(" main dispatch: state: %d, token: %d \n", state, token);
  43. #endif
  44.  
  45.     set_watch_cursor();
  46.     switch (state) {
  47.     /*--------------------------------------------*/
  48.     case IMG_UNLOADED:
  49.     switch (token) {
  50.     case LOAD:
  51.         if (load_image((char *) arg) == 1) {
  52.         fxn_init();
  53.         new_state(IMG_LOADED);
  54.         };
  55.         break;
  56.     case QUIT:
  57.         shutdown();
  58.         break;
  59.     default:
  60. #ifdef DEBUG
  61.         printf("inappropriate action.\n");
  62. #endif
  63.         break;
  64.     }
  65.     break;
  66.     /*--------------------------------------------*/
  67.     case IMG_LOADED:
  68.     switch (token) {
  69.     case LOAD:
  70.         clear_log();
  71.         fxn_reset();
  72.         if (load_image((char *) arg) == 1) {
  73.         fxn_init();
  74.         new_state(IMG_LOADED);
  75.         };
  76.         break;
  77.     case CLEAR:
  78.         if (curfunc->prev != NULL) {
  79.         log_del(curfunc->prev->id);
  80.         fxn_clear(curfunc->prev);
  81.         }
  82.         break;
  83.     case EVAL:
  84.         if (getrtype() == NOREG) {
  85.         log_perm();
  86.         fxn_eval();
  87.         fxn_reset();
  88.         fxn_init();
  89.         };
  90.         break;
  91.     case FORW:
  92.         adv_frame();
  93.         break;
  94.     case BACK:
  95.         rev_frame();
  96.         break;
  97.     case FMENU:
  98.         fxn_reset();
  99.         fxn_select(*((int *) arg));
  100.         fxn_init();
  101.         break;
  102.     case IMG_BUT:
  103.         if (event_is_down((Event *) arg)) {
  104.         if ((event_id((Event *) arg) == BUT(1))) {
  105.             if (getrtype() != NOREG) {
  106.             new_state(REG_SEL);
  107.             pt.x = dtoi(event_x((Event *) arg));
  108.             pt.y = dtoi(event_y((Event *) arg));
  109.             if ((line_mode == DRAG) && (getrtype() == LINE))
  110.                 start_line(pt.x, pt.y);
  111.             else
  112.                 add_point(pt.x, pt.y);
  113.             }
  114.         }
  115.         }
  116.         break;
  117.     case QUIT:
  118.         shutdown();
  119.         break;
  120.     default:
  121. #ifdef DEBUG
  122.         printf("Invalid action.\n");
  123. #endif
  124.         break;
  125.     }
  126.     break;
  127.     /*--------------------------------------------*/
  128.     case REG_SEL:
  129.     switch (token) {
  130.     case LOAD:
  131.         clear_plist();
  132.         clear_log();
  133.         fxn_reset();
  134.         if (load_image((char *) arg) == 1) {
  135.         fxn_init();
  136.         new_state(IMG_LOADED);
  137.         };
  138.         new_state(IMG_LOADED);
  139.         break;
  140.     case QUIT:
  141.         shutdown();
  142.     case EVAL:
  143.         do_eval();
  144.         break;
  145.     case CLEAR:
  146.         clear_plist();
  147.         new_state(IMG_LOADED);
  148.         break;
  149.     case IMG_BUT:
  150.         if (event_is_down((Event *) arg)) {
  151.         if (event_id((Event *) arg) == BUT(1)) {
  152.             pt.x = dtoi(event_x((Event *) arg));
  153.             pt.y = dtoi(event_y((Event *) arg));
  154.             add_point(pt.x, pt.y);    /* left button, add point */
  155.         }
  156.         if (event_id((Event *) arg) == BUT(2)) {
  157.             pt.x = dtoi(event_x((Event *) arg));
  158.             pt.y = dtoi(event_y((Event *) arg));
  159.             /* middle button, move point */
  160.             move_point((int) pt.x, (int) pt.y);
  161.         }
  162.         if (event_id((Event *) arg) == BUT(3)) {
  163.             pt.x = dtoi(event_x((Event *) arg));
  164.             pt.y = dtoi(event_y((Event *) arg));
  165.             del_point((int) pt.x, (int) pt.y);
  166.             if (getnpoints() == 0)    /* right button, remove point */
  167.             new_state(IMG_LOADED);
  168.         }
  169.         }
  170.         break;
  171.     default:
  172.         break;
  173.     }
  174.     break;
  175.     /*--------------------------------------------*/
  176.     case REG_EDIT:
  177.     switch (token) {
  178.     case EVAL:
  179.         do_edit_eval();
  180.         break;
  181.     case QUIT:
  182.         shutdown();
  183.         break;
  184.     case IMG_BUT:
  185.         if (event_is_down((Event *) arg)) {
  186.         if (event_id((Event *) arg) == BUT(1) ||
  187.             event_id((Event *) arg) == BUT(2)) {
  188.             pt.x = dtoi(event_x((Event *) arg));
  189.             pt.y = dtoi(event_y((Event *) arg));
  190.             /* left or middle button, move point */
  191.             move_point((int) pt.x, (int) pt.y);
  192.         }
  193. #ifdef NEED_LATER?
  194.         if (event_id((Event *) arg) == BUT(3)) {
  195.             pt.x = dtoi(event_x((Event *) arg));
  196.             pt.y = dtoi(event_y((Event *) arg));
  197.             /* right button, remove point */
  198.             del_point((int) pt.x, (int) pt.y);
  199.             if (getnpoints() == 0)
  200.             new_state(IMG_LOADED);
  201.         }
  202. #endif
  203.         }
  204.         break;
  205.     default:
  206.         break;
  207.     }
  208.     break;
  209.     default:
  210.     fprintf(stderr, "OUCH!!  Invalid _STATE_!!!\n");
  211.     exit(0);
  212.     break;
  213.     }
  214.     unset_watch_cursor();
  215.     return getstate();
  216. }
  217.  
  218. /*******************************************************************/
  219.  
  220. do_eval()
  221. {
  222.     if (getrtype() != NOREG) {
  223.     if (curfunc->reg == NULL) {
  224.         if (getrtype() == ENTIRE_IMAGE) {    /* create box region */
  225.         setrtype(BOX);
  226.         add_point(0, 0);
  227.         add_point(orig_img->width, orig_img->height);
  228.         }
  229.         if ((curfunc->reg = interp_reg()) == NULL) {
  230.         /* not enough points or some similar condition */
  231.         return;
  232.         }
  233.     } else {
  234.         /* make dlists from the plists */
  235.         if (build_dreg(curfunc->reg) < 0) {
  236.         /* didnt work */
  237.         printf("region draw didnt work\n");
  238.         return;
  239.         }
  240.     }
  241.     }
  242.     fxn_eval();
  243.     log_perm();
  244.     fxn_reset();
  245.     fxn_init();
  246.     new_state(IMG_LOADED);
  247. }
  248.  
  249. /*******************************************************************/
  250.  
  251. do_edit_eval()
  252. {
  253.     if (curfunc->reg == NULL) {
  254.     if ((curfunc->reg = interp_reg()) == NULL) {
  255.         /* not enough points or some similar condition */
  256.         return;
  257.     }
  258.     } else {
  259.     /* make dlists from the plists */
  260.     if (build_dreg(curfunc->reg) < 0) {
  261.         /* didnt work */
  262.         printf("region draw didnt work\n");
  263.         return;
  264.     }
  265.     }
  266.     fxn_change(curfunc->id);
  267. }
  268.  
  269. /*******************************************************************/
  270. /* set_panel_state() sets the appropriate items to active or inactive based on
  271.    the state */
  272. set_panel_state(s)
  273.     int       s;
  274. {
  275.     panel_deactivate();        /* turn everything off, then turn on what is
  276.                  * needed */
  277.     switch (s) {
  278.     case IMG_UNLOADED:
  279.     xv_set(base_win->file,
  280.            PANEL_INACTIVE, FALSE,
  281.            NULL);
  282.     xv_set(base_win->quit,
  283.            PANEL_INACTIVE, FALSE,
  284.            NULL);
  285.     break;
  286.     case IMG_LOADED:
  287.     xv_set(base_win->file,
  288.            PANEL_INACTIVE, FALSE,
  289.            NULL);
  290.     xv_set(base_win->quit,
  291.            PANEL_INACTIVE, FALSE,
  292.            NULL);
  293.     xv_set(base_win->clear,
  294.            PANEL_INACTIVE, FALSE,
  295.            NULL);
  296.     xv_set(base_win->clear_all,
  297.            PANEL_INACTIVE, FALSE,
  298.            NULL);
  299.     xv_set(base_win->func_sel,
  300.            PANEL_INACTIVE, FALSE,
  301.            NULL);
  302.     xv_set(base_win->reg_type,
  303.            PANEL_INACTIVE, FALSE,
  304.            NULL);
  305.     xv_set(base_win->refresh,
  306.            PANEL_INACTIVE, FALSE,
  307.            NULL);
  308.     xv_set(base_win->edit_mode,
  309.            PANEL_INACTIVE, FALSE,
  310.            NULL);
  311.     label_img(orig_img);
  312.     break;
  313.     case REG_SEL:
  314.     xv_set(base_win->file,
  315.            PANEL_INACTIVE, FALSE,
  316.            NULL);
  317.     xv_set(base_win->quit,
  318.            PANEL_INACTIVE, FALSE,
  319.            NULL);
  320.     xv_set(base_win->clear,
  321.            PANEL_INACTIVE, FALSE,
  322.            NULL);
  323.     xv_set(base_win->clear_all,
  324.            PANEL_INACTIVE, FALSE,
  325.            NULL);
  326.     xv_set(base_win->eval,
  327.            PANEL_INACTIVE, FALSE,
  328.            NULL);
  329.     xv_set(base_win->refresh,
  330.            PANEL_INACTIVE, FALSE,
  331.            NULL);
  332.     xv_set(base_win->edit_mode,
  333.            PANEL_INACTIVE, FALSE,
  334.            NULL);
  335.     break;
  336.     case REG_EDIT:
  337.     xv_set(base_win->quit,
  338.            PANEL_INACTIVE, FALSE,
  339.            NULL);
  340.     xv_set(base_win->eval,
  341.            PANEL_INACTIVE, FALSE,
  342.            NULL);
  343.     xv_set(base_win->edit_mode,
  344.            PANEL_INACTIVE, FALSE,
  345.            NULL);
  346.     break;
  347.     }
  348. }
  349.  
  350. /************************************************************/
  351. panel_deactivate()
  352. {
  353.     xv_set(base_win->file,
  354.        PANEL_INACTIVE, TRUE,
  355.        NULL);
  356.     xv_set(base_win->quit,
  357.        PANEL_INACTIVE, TRUE,
  358.        NULL);
  359.     xv_set(base_win->prev,
  360.        PANEL_INACTIVE, TRUE,
  361.        NULL);
  362.     xv_set(base_win->next,
  363.        PANEL_INACTIVE, TRUE,
  364.        NULL);
  365.     xv_set(base_win->func_sel,
  366.        PANEL_INACTIVE, TRUE,
  367.        NULL);
  368.     xv_set(base_win->reg_type,
  369.        PANEL_INACTIVE, TRUE,
  370.        NULL);
  371.     xv_set(base_win->eval,
  372.        PANEL_INACTIVE, TRUE,
  373.        NULL);
  374.     xv_set(base_win->clear,
  375.        PANEL_INACTIVE, TRUE,
  376.        NULL);
  377.     xv_set(base_win->clear_all,
  378.        PANEL_INACTIVE, TRUE,
  379.        NULL);
  380.     xv_set(base_win->refresh,
  381.        PANEL_INACTIVE, TRUE,
  382.        NULL);
  383.     xv_set(base_win->edit_mode,
  384.        PANEL_INACTIVE, TRUE,
  385.        NULL);
  386.     xv_set(base_win->edit_log_num,
  387.        PANEL_INACTIVE, TRUE,
  388.        NULL);
  389.     xv_set(base_win->edit_ok,
  390.        PANEL_INACTIVE, TRUE,
  391.        NULL);
  392. }
  393.